<%@page import="org.jdom.output.XMLOutputter"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.List"%>
<%@page import="org.jdom.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Meal And Wine Pairer</title>
<link href="style.css" rel="stylesheet" type="text/css" />


</head>

<body >
	<div id="content">
	<div id="header"></div>
	
	<div id="recipeSearchForm">
  		<span id="searchLabel">Recipe Search:</span>
        <form id="searchForm" name="searchForm" method="post" action="ProcessRecipeRequest">
          <input type="text" name="search" id="search" />
          <input type="submit" name="Sumbit" id="Sumbit" value="Submit"/>
        </form>
     </div>
     <div id="feed">   
           <%
           Document xmlDom = (Document) request.getAttribute("xmlDom");
           String passedFeedLink = (String) request.getAttribute("link");
           if(xmlDom != null){ 
           	
           	Namespace ns1 = Namespace.getNamespace("http://www.example.org");
           	Namespace def = Namespace.getNamespace("http://www.w3.org/2005/Atom");
           	Namespace recpup = Namespace.getNamespace("http://www.recipepuppy.com/about/api/");
           	Namespace snooth = Namespace.getNamespace("http://api.snooth.com");
           	
           	Element root = xmlDom.getRootElement();
           	
           	Element feedTitle = root.getChild("title", def);
           	String feedLink = null;
           	List feedLinks = root.getChildren("link",def);
           	for(int i = 0; i < feedLinks.size();i++){
           		if(((Element)feedLinks.get(i)).getAttribute("rel").getValue().equals("edit"))
           			feedLink = ((Element)feedLinks.get(i)).getAttribute("href").getValue();
           	}
           	if(feedLink.equals("#"))
           		feedLink = passedFeedLink;
           	List feedAuthors = root.getChildren("author", def);
           	List entries = root.getChildren("entry", def);
           	Element editURL = root.getChild("link", def);
           %>
           <div id="feedHeading">
           	<div id="feedTitle">
				Search Results: <%=feedTitle.getText() %>
           	</div>
           	<div id="feedAuthors">
           		
	           	<%
	           	for(int i = 0; i < feedAuthors.size(); i++){
	           		Element name = ((Element)feedAuthors.get(i)).getChild("name", def);
	           		Element email = ((Element)feedAuthors.get(i)).getChild("email", def);
	           	%>
	           	<div class="author">
		           	<b>Author:&nbsp;&nbsp;</b><%=name.getText() %>&nbsp;
		           	(<%=email.getText() %>)
	           	</div>
	           	<%} %>
	           	<br/>
	           	<br/>
           	</div>
           </div>
           <div class="entry">
           	<%
           	for(int k=0; k<entries.size(); k++){
           		Element entry = (Element)entries.get(k);	
           		Element entryTitle = entry.getChild("title", def);
               	//Element entryUpdate = entry.getChild("updated",def);
               	Element content = entry.getChild("content",def);
               	List pairings = content.getChildren("pairing",ns1);
           		String link = null;
               	List links = entry.getChildren("link",def);
               	for(int m = 0; m < links.size();m++)
               		if(((Element)links.get(m)).getAttribute("rel").getValue().equals("edit"))
               			link = ((Element)links.get(m)).getAttribute("href").getValue();
           	%>           	
           	<div class="pairing">
           		<%
           		for(int i = 0; i < pairings.size(); i++){
           	    	Element recipe = ((Element)pairings.get(i)).getChild("recipe",recpup);
           	    	Element recipeTitle = recipe.getChild("title",recpup);
           	    	Element recipeHref = recipe.getChild("href",recpup);
           	    	Element recipeIngredients = recipe.getChild("ingredients",recpup);
           	    	Element wines = ((Element)pairings.get(i)).getChild("wines",snooth);
           	    	List comments = ((Element)pairings.get(i)).getChildren("usercomment",ns1);
           	    %>
           	    <div class="recipe">
           	    	<a href="<%=recipeHref.getText() %>"><%=recipeTitle.getText() %></a>
           	    	<div class="recipeIngredients">
           	    		<span class="label">Ingredients:</span> <%=recipeIngredients.getText() %>
           	    	</div>
           		</div>
           		<div class="wines">
           			<%
           			List listWines = wines.getChildren("wine",snooth);
           			for(int j = 0; j < listWines.size(); j++){
           				Element wine = (Element)listWines.get(j);
           				Element wineName = wine.getChild("name",snooth);
           				Element wineRegion = wine.getChild("region",snooth);
           				Element winery = wine.getChild("winery",snooth);
           				Element varietal = wine.getChild("varietal",snooth);
           				Element price = wine.getChild("price",snooth);
           				Element vintage = wine.getChild("vintage",snooth);
           				Element type = wine.getChild("type",snooth);
           				Element link1 = wine.getChild("link",snooth);
           				Element snoothrank = wine.getChild("snoothrank",snooth);
           				Element wineImageURL = wine.getChild("image", snooth);
           				
           			%>
           			<div class="wineImage">
           				<img src="<%= wineImageURL.getText() %>" />
           			</div>
           			<div class="wine">
           				<a href="<%=link1.getText()%>" class="wineName"><%=wineName.getText()%> (<%=vintage.getText() %>)</a>
           				<div class="wineInfo">
	           				<div class="winery">
	           					<span class="label">Winery:</span> <%=winery.getText() %>
	           				</div>
	           				<div class="wineRegion">
	           					<span class="label">Region:</span> <%=wineRegion.getText() %>
	           				</div>
	           				<div class="varietal">
	           				  	<span class="label">Varietals:</span> <%=varietal.getText() %>
	           				</div>
	           				<div class="type">
	           					<span class="label">Type:</span> <%=type.getText() %>
	           				</div>
	           				<div class="price">
	           					<span class="label">Price:</span> <%=price.getText()%>
	           				</div>
	           				<div class="snoothrank">
	           				 	<span class="label">Rank:</span> <%=snoothrank.getText() %>
	           				</div>
	           			</div>
           			</div>
           			<%}%>
           		</div>
           		<div class="userCommentsTitle">User Pairing Comments</div>
           		<div class="comments">
           		<%for(int c=0; comments !=null && c<comments.size(); c++){%>
           			<div class="comment">
           				<span class="label">Comment:</span> <%=((Element)comments.get(c)).getText()%>
           			</div>
           		<%} %>
           		
           		<form class="commentForm" method="post" action="UpdateComment">
             			<textarea cols="70" rows="4" name="comment">Enter a comment.</textarea>
             			<input name="entrylink"  type="hidden" value="<%=link%>"/>
             			<input name="feedlink"  type="hidden" value="<%=feedLink%>"/>
             			<br/>
             			<input type="submit" name="Submit"  value="Submit Comment" />
          		</form>
          		</div>
           		<%} %>
           		<div class="spacer"></div> 
           		<%} %>
           	</div>
           </div>
           
           <%} %>
         </div>
         <div id="footer"><p>Copyright &copy; 2011 Perfect Pairing. All Rights Reserved</p></div>
         </div>
</body>
</html>
